package com.hsh.tools.convert;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public abstract class ConvertUtils {

    /**
     * 对象转map
     */
    public static Map<String, String> objectConvertMap(Object obj) {
        if (obj == null) {
            return null;
        }
        Class clazz = obj.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        Map<String, String> result = new HashMap<>(declaredFields.length);
        for (Field field : declaredFields) {
            String fieldName = field.getName();
            field.setAccessible(true);
            try {
                if (field.get(obj) != null) {
                    result.put(fieldName, field.get(obj).toString());
                } else {
                    result.put(fieldName, null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
